import Operator from "./Operator";
import {RC} from "../config/RC";

class OperatorDecorator extends Operator {
    private _child_operator: Operator;   // 待装饰的对象，相当于链表中的下一个节点
    private _operator !: Operator;       // 本次要执行的操作

    constructor(operator: Operator, child_operator: Operator) {
        super();
        this._operator = operator;
        this._child_operator = child_operator;
    }

    public getChildOperator(): Operator {
        return this._child_operator;
    }

    public getOperator(): Operator {
        return this._operator;
    }

    public async execute(): Promise<RC> {
        try {
            let rc = await this._child_operator.execute();
            if (rc !== RC.SUCCESS) {
                return rc;
            } else {
                this._operator.setOperateArgs(this._child_operator.getOperateArgs());
                rc = await this._operator.execute();
                this.setOperateArgs(this._operator.getOperateArgs());
                return rc;
            }
        } catch (error) {
            console.error("执行操作失败:", error);
            throw error;
        }
    }
}

export default OperatorDecorator;